package com.itggm.controller;
import com.itggm.pojo.Dept;
import com.itggm.pojo.Result;
import com.itggm.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService DeptService;
    @GetMapping()
    //1.1 获取所有部门数据
    public Result list(){
       log.info("查询所有部门数据");
       List<Dept> deptList= DeptService.list();
       return Result.success(deptList);
    }
    //1.2 删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门，id：{}",id);
        DeptService.delete(id);
        return Result.success();
    }
    //1.3 新增部门
    @PostMapping()
    public Result add(@RequestBody Dept dept){
        log.info("新增部门，部门数据：{}",dept);
        DeptService.add(dept);
        return Result.success();
    }
    //1.4.1 修改部门数据，先通过get请求获取要修改的部门id，再根据id修改部门信息
    @GetMapping ("/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("获取部门ID:{}",id);
        Dept dept = DeptService.getByID(id);
        return Result.success(dept);
    }
    //1.4.2 修改部门信息
    @PutMapping("")
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        DeptService.update(dept);
        return Result.success();
    }

}
